package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 权限服务 实现类
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService{

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分页查询权限数据
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    public PageResult find(Integer currentPage, Integer pageSize, String queryString) {

        /*使用分页插件进行分页查询 核心代码就这一行，页数、每页行数*/
        PageHelper.startPage(currentPage,pageSize);
        /*插件工具用法需要另外查询理解*/
        Page<Permission> page = permissionDao.selectByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 新增权限
     *
     * @param permission
     */
    public void add(Permission permission) {

        permissionDao.add(permission);

    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     * 删除权限项
     *
     * @param id
     */
    public void delete(Integer id) {

        Long count = permissionDao.findCountById(id);
        if (count > 0) {
            throw new RuntimeException("有角色关联此权限，删除失败");
        }

        permissionDao.delete(id);

    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
